class ReportStatistic < ActiveRecord::Base
  belongs_to :report
  
  def self.get_salary_upper(salary)
    n_salary=ReportStatistic.log_amount(salary)
    int_salary=ReportStatistic.sma_amount(salary,n_salary)+1
    return ReportStatistic.lag_amount(int_salary,n_salary)
  end
  
  def self.get_salary_lower(salary)
    n_salary=ReportStatistic.log_amount(salary)
    int_salary=ReportStatistic.sma_amount(salary,n_salary)-1
    return ReportStatistic.lag_amount(int_salary,n_salary)
  end
  
private
  def self.log_amount(amount)
    return Math.log10(amount).to_i
  end
  
  def self.sma_amount(amount,n)
    if n>=0
      n.times {amount/=10}
      return amount
    else
      return 1
    end
  end
  
  def self.lag_amount(amount,n)
    if n>=0
      n.times {amount*=10}
      return amount
    else
      return 1
    end
  end
  
end

class ReportEducation < ReportStatistic
  belongs_to :education, :foreign_key=>"upper"
  def self.create_from_incumbent(incumbent)
    report_education=ReportStatistic::ReportEducation.new
    report_education.education=incumbent.report_education
    report_education.value=1
    return report_education
  end
end

class ReportOversea < ReportStatistic
  belongs_to :oversea, :foreign_key=>"upper"

  def self.create_from_incumbent(incumbent)
    report_oversea=ReportStatistic::ReportOversea.new
    report_oversea.oversea=incumbent.report_oversea
    report_oversea.value=1
    return report_oversea
  end
  
end

class ReportSalary < ReportStatistic

  def self.create_from_incumbent(incumbent)
    report_salary = ReportStatistic::ReportSalary.new
    report_salary.upper=ReportSalary.get_salary_upper(incumbent.salary)
    report_salary.lower=ReportSalary.get_salary_lower(incumbent.salary)
    report_salary.value=1
    return report_salary
  end
end
